package com.tender.service.impl;

import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.lang.Assert;
import com.tender.domain.ShiroUserRoleExample;
import com.tender.domain.ShiroUserRole;
import com.tender.domain.ShiroUserRoleCondition;
import com.tender.mapper.ShiroUserRoleMapper;
import com.tender.service.ShiroUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import com.tender.utils.DataUtils;

@Service
public class ShiroUserRoleServiceImpl implements ShiroUserRoleService {

    @Autowired
    private ShiroUserRoleMapper shiroUserRoleMapper;

    @Override
    public ShiroUserRole selectOne(ShiroUserRoleCondition param) {
        Assert.notNull(param.getRoleRowId(), "角色rowId不能为空");
        Assert.notNull(param.getUserRowId(), "用户rowId不能为空");

        ShiroUserRoleExample example = new ShiroUserRoleExample();
        ShiroUserRoleExample.Criteria criteria = example.createCriteria();

        DataUtils.setLongValueIfNotNull(param.getUserRowId(), criteria::andUserRowIdEqualTo);
        DataUtils.setLongValueIfNotNull(param.getRoleRowId(), criteria::andRoleRowIdEqualTo);

        List<ShiroUserRole> shiroUserRoles = shiroUserRoleMapper.selectByExample(example);
        return CollectionUtils.isEmpty(shiroUserRoles) ? null : shiroUserRoles.get(0);
    }

    @Override
    public List<ShiroUserRole> selectList(ShiroUserRoleCondition param) {

        ShiroUserRoleExample example = new ShiroUserRoleExample();
        ShiroUserRoleExample.Criteria criteria = example.createCriteria();

        DataUtils.setListIfNotEmpty(param.getRoleRowIdList(), criteria::andRoleRowIdIn);
        DataUtils.setListIfNotEmpty(param.getUserRowIdList(), criteria::andUserRowIdIn);
        DataUtils.setLongValueIfNotNull(param.getUserRowId(), criteria::andUserRowIdEqualTo);

        List<ShiroUserRole> shiroUserRoles = shiroUserRoleMapper.selectByExample(example);

        return CollectionUtils.isEmpty(shiroUserRoles) ? new ArrayList<>() : shiroUserRoles;
    }

    @Override
    public int updateByCondition(ShiroUserRoleCondition param) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(ShiroUserRoleCondition param) {
        return 0;
    }

    @Override
    public int insertSelective(ShiroUserRole param) {
        return 0;
    }

    @Override
    public int insertListSelective(List<ShiroUserRole> params) {
        return 0;
    }

    @Override
    public int deleteByCondition(ShiroUserRoleCondition param) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Long rowId) {
        Assert.notNull(rowId, "用户角色rowId不能为空");
        ShiroUserRoleExample example = new ShiroUserRoleExample();
        ShiroUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRowIdEqualTo(rowId);

        return shiroUserRoleMapper.deleteByExample(example);
    }
}
